package com.guxuede.mina.chat;

import java.util.HashMap;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;

import com.guxuede.mina.bean.HttpMessage;

public class ChatPro {

	public static void chatSessionLeave(IoSession session){
		ChatHall.leaveRoom(session);
	}
	
	public static void chatSessionReceived(HttpMessage request,IoSession session){
		HashMap<String,String> data=request.data;
		String opt=data.get("opt");
		if(opt!=null && opt.equalsIgnoreCase("join")){
			ChatHall.joinRoom(session);
			ChatRoom.speakTo("<comet>Hello,welcome to join!</comet>", session);
		}else if(opt!=null && opt.equalsIgnoreCase("send")){
			String msg=data.get("msg");
			String nick=data.get("nick")+":";
			if(msg!=null){
				ChatHall.speakAll(0L, "<comet>"+nick+msg+"</comet>");
			}
			session.close(false);
		}
		else{
			session.close(false);
		}
	}
	
	public static void main(String[] args) {
		IoBuffer buf=IoBuffer.allocate(10);
		buf.putChar('i');
		System.out.println(buf);
		buf.rewind();
		System.out.println(buf);
	}
}
